package config;
import java.io.File;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.mchange.v2.c3p0.cfg.C3P0Config;

import controller.*;
import interceptor.LoginInterceptor;
import model.*;

/**
 * Created by BaiYunfei on 2017/6/14.
 */
public class Config extends JFinalConfig {

    @Override
    public void configConstant(Constants constants) {
        constants.setDevMode(true);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/", IndexController.class, "/WEB-INF/pages");
        routes.add("/student",StudentController.class, "/WEB-INF/pages/student");
        routes.add("/teacher", TeacherController.class, "/WEB-INF/pages/teacher");
        routes.add("/course", CourseController.class, "/WEB-INF/pages/course");
        routes.add("/sc", SelectCourseController.class, "/WEB-INF/pages/sc");
        routes.add("/ratingScale", RatingScaleController.class, "/WEB-INF/pages/ratingScale");
    }

    @Override
    public void configEngine(Engine engine) {
        engine.addSharedFunction("/WEB-INF/pages/template/_tabLayout.html");
    }

    @Override
    public void configPlugin(Plugins plugins) {
        PropKit.use("config.properties");

        C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("db_url"),
                PropKit.get("db_username"),
                PropKit.get("db_password"),
                PropKit.get("db_driver"));

        plugins.add(c3p0Plugin);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);

        arp.setDialect(new SqlServerDialect());
        arp.setShowSql(true);
        
        arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
        arp.addSqlTemplate("sc.sql");
        arp.addSqlTemplate("ratingScale.sql");
        arp.addSqlTemplate("course.sql");

        arp.addMapping("[User]","ID", User.class);
        arp.addMapping("Student", "Student_ID", Student.class);
        arp.addMapping("Teacher", "Teacher_ID", Teacher.class);
        arp.addMapping("Course", "Course_ID", Course.class);
        arp.addMapping("SC", StudentCourse.class);
        arp.addMapping("RatingScale", RatingScale.class);


        plugins.add(arp);
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new LoginInterceptor());
    }

    @Override
    public void configHandler(Handlers handlers) {

    }

    public static void main(String[] args) {
        JFinal.start("web",8080,"/",5);
    }

}
